package com.gangwangchina.android.loveynotify;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.KeyguardManager.OnKeyguardExitResult;
import android.content.Context;

public class ManageKeyguard {
	private static KeyguardManager myKM = null;

	private static KeyguardLock myKL = null;

	public static synchronized void initialize(Context context) {
		if (myKM == null) {
			myKM = (KeyguardManager) context
					.getSystemService(Context.KEYGUARD_SERVICE);
		}
	}

	public static synchronized void disableKeyguard(Context context) {
		// myKM = (KeyguardManager)
		// context.getSystemService(Context.KEYGUARD_SERVICE);
		initialize(context);

		if (myKM.inKeyguardRestrictedInputMode()) {
			myKL = myKM.newKeyguardLock("LoveyNotify");
			myKL.disableKeyguard();
		} else {
			myKL = null;
		}
	}

	public static synchronized boolean inKeyguardRestrictedInputMode() {
		if (myKM != null) {
			return myKM.inKeyguardRestrictedInputMode();
		}
		return false;
	}

	public static synchronized void reenableKeyguard() {
		if (myKM != null) {
			if (myKL != null) {
				myKL.reenableKeyguard();
				myKL = null;
			}
		}
	}

	public static synchronized void exitKeyguardSecurely(
			final LaunchOnKeyguardExit callback) {
		if (inKeyguardRestrictedInputMode()) {
			myKM.exitKeyguardSecurely(new OnKeyguardExitResult() {
				public void onKeyguardExitResult(boolean success) {
					reenableKeyguard();
					if (success) {
						callback.LaunchOnKeyguardExitSuccess();
					} else {
					}
				}
			});
		} else {
			callback.LaunchOnKeyguardExitSuccess();
		}
	}

	public interface LaunchOnKeyguardExit {
		public void LaunchOnKeyguardExitSuccess();
	}
}
